<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        $this->createAdminTable();
        $this->createAdminInfoTable();
        $this->createAdminSessionTable();
    }

    public function down(): void
    {
        Schema::dropIfExists('admin_session');
        Schema::dropIfExists('admin_info');
        Schema::dropIfExists('admin');
    }

    private function createAdminTable(): void
    {
        Schema::create('admin', function (Blueprint $table) {
            $table->id();
            $table->string('username', 50)->unique()->comment('用户名');
            $table->string('password', 255)->comment('密码');
            $table->string('nickname', 50)->nullable()->comment('昵称');
            $table->string('avatar', 255)->nullable()->comment('头像');
            $table->string('email', 100)->nullable()->comment('邮箱');
            $table->string('mobile', 20)->nullable()->comment('手机号');
            $table->tinyInteger('status')->default(1)->comment('状态：0=禁用，1=启用');
            $table->string('last_login_ip', 50)->nullable()->comment('最后登录IP');
            $table->timestamp('last_login_time')->nullable()->comment('最后登录时间');
            $table->rememberToken();
            $table->timestamps();
            $table->softDeletes();
        });
    }

    private function createAdminInfoTable(): void
    {
        Schema::create('admin_info', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('admin_id')->comment('管理员ID');
            $table->string('real_name', 50)->nullable()->comment('真实姓名');
            $table->tinyInteger('gender')->default(0)->comment('性别：0=未知，1=男，2=女');
            $table->date('birthday')->nullable()->comment('生日');
            $table->string('address', 255)->nullable()->comment('地址');
            $table->string('id_card', 18)->nullable()->comment('身份证号');
            $table->string('emergency_contact', 50)->nullable()->comment('紧急联系人');
            $table->string('emergency_phone', 20)->nullable()->comment('紧急联系电话');
            $table->text('remark')->nullable()->comment('备注');
            $table->timestamps();
            $table->softDeletes();

            $table->foreign('admin_id')->references('id')->on('admin')->onDelete('cascade');
        });
    }

    private function createAdminSessionTable(): void
    {
        Schema::create('admin_session', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('admin_id')->comment('管理员ID');
            $table->string('token', 100)->unique()->comment('会话令牌');
            $table->string('ip', 50)->comment('登录IP');
            $table->string('user_agent', 255)->nullable()->comment('用户代理');
            $table->timestamp('login_time')->comment('登录时间');
            $table->timestamp('last_activity')->comment('最后活动时间');
            $table->timestamps();

            $table->foreign('admin_id')->references('id')->on('admin')->onDelete('cascade');
            $table->index(['admin_id', 'token']);
        });
    }
}; 